<!-- @module biz-travel -->
<!-- @author: YunTao.Li -->
<!-- @description: 出差申请单 -->
<!-- @since: 2019-11-06 23:43:56 -->
<template>
  <el-card  class="box-card el-card-flex" >
  <div slot="header" class="clearfix">
    <span>
        <i class="el-icon-position"></i>
         查询信息
      </span>
  </div>
    <!-- form begin -->
    <div style="margin: 10px" >
    <el-form ref="queryForm" label-width="120px" class="div-header" :model="queryForm" size="mini">
        <el-row :gutter="10">
          <el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
            <el-form-item label="组织" prop="ouId">
              <!-- <el-input v-model="queryForm.ouId" placeholder="组织" @getValue="getValue($event)" /> -->
              <OrgSelectTree v-model="queryForm.orgCode" style="width:240px;" placeholder="组织" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
            <el-form-item label="单据编号" prop="bizTravelCode">
              <el-input v-model="queryForm.bizTravelCode" style="width:240px;" placeholder="单据编号" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
            <el-form-item label="审批状态" prop="businessStatus">
              <dictCombo v-model="queryForm.businessStatus" style="width:240px;" dicttypeid="businessStatus" name="businessStatus" placeholder="审批状态" />
            </el-form-item>
          </el-col>
        </el-row>
        <div class="btnStyle">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="getList()"
          >查询</el-button>
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-refresh"
            @click="resetForm('queryForm')"
          >重置</el-button>
        </div>
    </el-form>
    </div>

    <!-- form end -->
      <div class="div-button">
        <el-button
          icon="el-icon-menu"
          type="primary"
          :disabled.sync="btn_view_disabled"
          size="mini"
          @click="onView"
        >查看详情</el-button>
        <el-button
          icon="el-icon-menu"
          type="success"
          :disabled.sync="btn_add_disabled"
          size="mini"
          @click="onAdd"
        >新增</el-button>
        <el-button
          icon="el-icon-menu"
          type="warning"
          size="mini"
          :disabled.sync="btn_edit_disabled"
          @click="onEdit"
        >编辑</el-button>
        <el-button
          icon="el-icon-menu"
          type="danger"
          size="mini"
          :disabled.sync="btn_delete_disabled"
          @click="handleDelete"
        >删除</el-button>
        <el-button
          icon="el-icon-menu"
          type="primary"
          size="mini"
          :disabled.sync="btn_submit_disabled"
          @click="handleSubmit"
        >提交</el-button>
        <el-button
          icon="el-icon-menu"
          type="info"
          size="mini"
          :disabled.sync="btn_drawback_disabled"
          @click="handleDrawback"
        >撤回</el-button>
        <el-button
          icon="el-icon-menu"
          type="primary"
          size="mini"
          :disabled.sync="btn_monitor_disabled"
          @click="onTaskMonitor"
        >监控</el-button>
      </div>

      <!-- table begin-->
      <div class="el-flex el-box-column " style="margin: 10px;">
        <el-card class="box-card el-card-flex" >
        <div slot="header" class="clearfix">
        <span>
        <i class="el-icon-position"></i>
          因事外出查询
        </span>
        </div>
        <div class="el-flex el-box-column" style="margin: 5px;" >
          <el-table v-loading="listLoading" :data="leaveList"
                    border fit highlight-current-row class="tableStyle"
                    height="100%" style="width: 100%" @selection-change="onSelectChange">

            <el-table-column type="selection" width="55" />
            <el-table-column align="center" type="index" width="55" label="序号" />
            <!--ID-->
            <!-- <el-table-column align="center" label="序号" width="150px">
              <template slot-scope="scope">
                <span>{{ scope.row.bizTravelId }}</span>
              </template>
            </el-table-column> -->
            <!--单据编号-->
            <el-table-column align="center" label="单据编号" min-width="200px">
              <template slot-scope="scope">
                <span>{{ scope.row.bizTravelCode }}</span>
              </template>
            </el-table-column>
            <!--出差事由-->
            <el-table-column align="center" label="出差事由" min-width="250px">
              <template slot-scope="scope">
                <span>{{ scope.row.travelDesc }}</span>
              </template>
            </el-table-column>
            <!--申请人-->
            <el-table-column  align="center" label="申请人" min-width="100px">
              <template slot-scope="scope">
                <span>{{ scope.row.empName }}</span>
              </template>
            </el-table-column>
            <!--预计起始时间-->
            <el-table-column  align="center" label="预计起始时间" min-width="120px">
              <template slot-scope="scope">
                <span>{{ scope.row.startDate | parseTime('{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <!--预计开始时段-->
            <el-table-column prop="startTimeType" :formatter="dictFormatter" label-class-name="timeType" align="center" label="预计开始时段" min-width="100px">
              <!-- <template slot-scope="scope">
                <span>{{ scope.row.startTimeType }}</span>
              </template> -->
            </el-table-column>
            <!--预计终止时间-->
            <el-table-column  align="center" label="预计终止时间" min-width="120px">
              <template slot-scope="scope">
                <span>{{ scope.row.endDate | parseTime('{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <!--预计终止时段-->
            <el-table-column prop="endTimeType" :formatter="dictFormatter" label-class-name="timeType" align="center" label="预计终止时段" min-width="100px">
              <!-- <template slot-scope="scope">
                <span>{{ scope.row.endTimeType }}</span>
              </template> -->
            </el-table-column>
            <!--审批状态-->
            <el-table-column prop="businessStatus" :formatter="dictFormatter" label-class-name="businessStatus"  label="审批状态" align="center" min-width="80px">
              <!-- <template slot-scope="scope">
                <span>{{ scope.row.businessStatus }}</span>
              </template> -->
            </el-table-column>
            <!--实际起始时间-->
            <el-table-column  align="center" label="实际起始时间" min-width="150px">
              <template slot-scope="scope">
                <span>{{ scope.row.actualStartDate | parseTime('{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <!--实际开始时段-->
            <el-table-column prop="actualStartTimeType" :formatter="dictFormatter" label-class-name="timeType" align="center" label="实际开始时段" min-width="100px" />
            <!--实际终止时间-->
            <el-table-column  align="center" label="实际终止时间" min-width="150px">
              <template slot-scope="scope">
                <span>{{ scope.row.actualEndDate | parseTime('{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <!--实际终止时段-->
            <el-table-column prop="actualEndTimeType" :formatter="dictFormatter" label-class-name="timeType" align="center" label="实际终止时段" min-width="100px" />
            <!--变更事由-->
            <el-table-column align="center" label="变更事由" min-width="80px">
              <template slot-scope="scope">
                <span>{{ scope.row.changeDesc }}</span>
              </template>
            </el-table-column>

          </el-table>
        </div>
      <!-- table end -->
      <pagination background
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="total" :page.sync="queryForm.page"
                  :limit.sync="queryForm.limit" @pagination="getList" />

        </el-card>
        <travelForm ref="travelInputDialog" @submitFunc="onSubmitForm" />
      <travelView ref="travelViewDialog" @submitFunc="onSubmitForm" />
      <historyMonitorList ref="hisMonitorDialog" />
      </div>
  </el-card>
</template>

<script>
import { travelList, travelAdd, travelUpdate, travelDelete, travelSubmit, travelDrawback } from '@/api/attendance/travel-service'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import travelForm from './components/form/travel-form'
import travelView from './components/form/travel-detail'
import dictCombo from './components/combo/dict-comb'
import historyMonitorList from '@/views/attendance/components/taskMonitorList'

import OrgSelectTree from './components/combo/org-tree-select.vue'

export default {
  components: { Pagination, travelForm, travelView, dictCombo, OrgSelectTree, historyMonitorList },
  data() {
    return {
      msg: '-',
      text_form: '查询信息',
      text_grid: '因事外出查询',
      btn_add_disabled: false,
      btn_view_disabled: true,
      btn_delete_disabled: true,
      btn_edit_disabled: true,
      btn_submit_disabled: true,
      btn_drawback_disabled: true,
      btn_monitor_disabled: true,
      leaveList: [],
      listLoading: true,
      total: 0,
      selectedLength: 0,
      lastSelectedData: null,
      queryForm: {
        page: 1,
        limit: 20,
        ouId: '',
        orgCode: '',
        deptName: '',
        deptCode: '',
        empName: '',
        empCode: '',
        bizTravelCode: '',
        businessStatus: '',
        queryRange: ''
      }, inputRules: {
      },
      height: 'calc(100% - 16px)'
    }
  },
  created() {
    // init query range
    const queryRange = localStorage.getItem('queryRange')
    this.queryForm.queryRange = queryRange
    if (queryRange === 'all') {
      this.btn_add_disabled = true
    } else if (queryRange === 'dept') {
      try {
        this.queryForm.deptName = localStorage.getItem('deptName')
        this.queryForm.deptCode = localStorage.getItem('deptCode')
        this.queryForm.orgCode = localStorage.getItem('orgCode')
      } catch (e) {
        console.log(e)
      }
    } else if (queryRange === 'person') {
      try {
        this.queryForm.empCode = localStorage.getItem('empCode')
        this.queryForm.empName = localStorage.getItem('empName')
        this.queryForm.orgCode = localStorage.getItem('orgCode')
      } catch (e) {
        console.log(e)
      }
    }

    // 初始化查询展示
    this.getList()
    // 初始化按钮diabled状态
    this.onSelectChange({})
  },
  methods: {
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // getValue(value) {
    //   this.queryForm.ouId = value
    //   console.log(value)
    // },
    getList() {
      this.listLoading = true
      travelList(this.queryForm).then(response => {
        this.leaveList = response.data.records
        this.total = response.data.total

        // 假loading 1秒后消失
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      })
    },
    inputFormToggle(travelData) {
      this.$refs.travelInputDialog.toggele(travelData)
    },
    viewFormToggle(travelData) {
      this.$refs.travelViewDialog.toggele(travelData)
    },
    handleAdd(data) { // 添加方法
      travelAdd(data)
        .then(response => {
          if (response.code === 200) {
            this.getList()
            // 清理子组件
            this.$refs.travelInputDialog.resetFormData()
          } else {
            this.$message.error(response.message)
          }
        })
    },
    handleEdit(data) { // 编辑方法
      travelUpdate(data.bizTravelId, data)
        .then(response => {
          if (response.code === 200) {
            this.getList()
            // 清理子组件
            this.$refs.travelInputDialog.resetFormData()
          } else {
            this.$message.error(response.message)
          }
        })
    },
    handleDelete() { // 删除方法
      const _this = _this || this
      const travelId = _this.lastSelectedData.bizTravelId

      this.$confirm('确定要删除么?', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        travelDelete(travelId).then(response => {
          if (response.code === 200) {
            this.$message({ type: 'success', message: '删除成功!' })
            this.getList()
          } else {
            this.$message.error(response.message)
          }
        })
      }).catch(err => { console.error(err) })
    },
    handleSubmit() { // 提交请假单
      this.$confirm('确定要提交么?', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        travelSubmit(this.lastSelectedData.bizTravelId, this.lastSelectedData).then(response => {
          if (response.code === 200) {
            this.$message({ type: 'success', message: '提交成功!' })
            this.getList()
          } else {
            this.$message.error(response.message)
          }
        })
      }).catch(err => { console.error(err) })
    },
    handleDrawback() {
      this.$confirm('确定要撤回么?', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        travelDrawback(this.lastSelectedData.bizTravelId, this.lastSelectedData).then(response => {
          if (response.code === 200) {
            this.$message({ type: 'success', message: '撤回成功!' })
            this.getList()
          } else {
            this.$message.error(response.message)
          }
        })
      }).catch(err => { console.error(err) })
    },
    onTaskMonitor() {
      const selectedData = JSON.parse(JSON.stringify(this.lastSelectedData))
      this.$refs.hisMonitorDialog.toggele(selectedData)
    },
    onAdd() {
      this.inputFormToggle(null)
    },
    onView() {
      const travelData = JSON.parse(JSON.stringify(this.lastSelectedData))
      this.viewFormToggle(travelData)
    },
    onEdit() {
      // this.$refs.travelInputDialog.travelInputForm
      const travelData = JSON.parse(JSON.stringify(this.lastSelectedData))
      this.inputFormToggle(travelData)
    },
    onSubmitForm(data) { // 表单提交事件
      const submitTravelId = data.bizTravelId
      if (submitTravelId === null || submitTravelId === '' || JSON.stringify(submitTravelId) === '{}') {
        this.handleAdd(data)
      } else {
        this.handleEdit(data)
      }
    },
    onSelectChange(selection) {
      this.selectedLength = selection.length || 0
      this.lastSelectedData = selection[this.selectedLength - 1]

      this.btn_delete_disabled = true
      this.btn_edit_disabled = true
      this.btn_view_disabled = true
      this.btn_submit_disabled = true
      this.btn_drawback_disabled = true
      this.btn_monitor_disabled = true

      // 先判断queryRange
      const queryRange = this.queryForm.queryRange
      if (queryRange === 'all' || queryRange === 'dept') {
        if (this.selectedLength && this.selectedLength === 1) {
          this.btn_view_disabled = false
        }
        return
      }

      // 根据审批状态改变按钮行为
      if (!this.lastSelectedData) {
        return
      }

      const businessStatus = this.lastSelectedData.businessStatus
      if (this.selectedLength && this.selectedLength === 1) {
        this.btn_view_disabled = false
        if (businessStatus && businessStatus === 'A') { // 待提交，草稿
          this.btn_edit_disabled = false
          this.btn_delete_disabled = false
          this.btn_submit_disabled = false
        } else if (businessStatus && businessStatus === 'B') { // 已撤回
          this.btn_edit_disabled = false
          this.btn_submit_disabled = false
          this.btn_monitor_disabled = false
        } else if (businessStatus && businessStatus === 'D') {
          // 已驳回
          this.btn_edit_disabled = false
          this.btn_submit_disabled = false
          this.btn_monitor_disabled = false
        } else if (businessStatus && businessStatus === 'C') { // 已提交，待审批
          this.btn_drawback_disabled = false
          this.btn_monitor_disabled = false
        } else if (businessStatus && businessStatus === 'E') { // 已提交，已审批
          this.btn_monitor_disabled = false
        } else if (businessStatus && businessStatus === 'F') { // 已提交，审批中
          this.btn_monitor_disabled = false
        }
      }
    }
  }
}
</script>

<style scoped lang="scss">
@import "./index.scss";
</style>
